Component({
  data: {
    date: "",
    show: false,
    comName: "calendar-demo-01",
    formatter(day: any) {
      const year = day.date.getFullYear();
      const month = day.date.getMonth() + 1;
      const date = day.date.getDate();

      const curDate = new Date();
      if (
        year === curDate.getFullYear() &&
        month === curDate.getMonth() + 1 &&
        date === curDate.getDate()
      ) {
        day.text = "今天";
      }

      if (month === 5) {
        if (date === 1) {
          day.topInfo = "劳动节";
        } else if (date === 4) {
          day.topInfo = "五四青年节";
        }
      }

      if (day.type === "start") {
        day.bottomInfo = "入住";
      } else if (day.type === "end") {
        day.bottomInfo = "离店";
      }

      return day;
    },
  },
  lifetimes: {
    created() {
      console.log(`${this.data.comName} created`);
    },
    ready() {
      console.log(`${this.data.comName} ready`);
    },
    attached() {
      console.log(`${this.data.comName} attached`);
    },
    moved() {
      console.log(`${this.data.comName} moved`);
    },
  },
  pageLifetimes: {
    hide() {
      console.log(`${this.data.comName} hide`);
    },
    resize() {
      console.log(`${this.data.comName} resize`);
    },
    show() {
      console.log(`${this.data.comName} show`);
    },
  },
  methods: {
    onDisplay() {
      this.setData({ show: true });
    },
    onClose() {
      this.setData({ show: false });
    },
    formatDate(date: any) {
      const d = new Date(date);
      return `${d.getMonth() + 1}/${d.getDate()}`;
    },
    onConfirm(event: WechatMiniprogram.CustomEvent) {
      this.setData({
        show: false,
        date: this.formatDate(event.detail),
      });
    },
  },
});
